﻿using System.Data.SqlClient;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules._2DoTasks.Components.Entities;
using DotNetNuke.Modules._2DoTasks.Components.ExceptionHandling;
using DotNetNuke.Modules._2DoTasks.Components.Interfaces;
using DotNetNuke.Modules._2DoTasks.Data;

namespace DotNetNuke.Modules._2DoTasks.Components.Controllers
{
    /// <summary>
    /// Controller object with business logic for TaskTimes
    /// </summary>
    public class TaskTimeController : IRepository<_TaskTime, int>
    {
        private UnitOfWork _uof;
        private int _taskId;

        #region IRepository<_TaskTime> Members

        public TaskTimeController(UnitOfWork uof, int taskId)
        {
            _uof = uof;
            this._taskId = taskId;
        }

        /// <summary>
        /// Gets the TaskTime with the passed id
        /// </summary>
        /// <param name="TaskTimeId">the TaskTime id</param>
        /// <param name="additionalFields">not used in TaskTime entity</param>
        /// <returns>returns a TaskTime object</returns>
        public _TaskTime Get(int TaskTimeId, params object[] additionalFields)
        {
            return CBO.FillObject<_TaskTime>(DataProvider.Instance().GetTaskTime(TaskTimeId, this._taskId, this._uof.CurrentUser.UserID));
        }

        /// <summary>
        /// Returns all TaskTimes
        /// </summary>
        /// <returns>retuns list of TaskTimes</returns>
        public System.Collections.Generic.List<_TaskTime> GetAll()
        {
            return CBO.FillCollection<_TaskTime>(DataProvider.Instance().GetAllTaskTimeEntries(this._taskId, this._uof.CurrentUser.UserID));
        }

        /// <summary>
        /// Adds a new entity
        /// </summary>
        /// <param name="entity">entity to create</param>
        public void Add(_TaskTime entity)
        {
            try
            {
                entity.TaskId = this._taskId;
                entity.CreationUserId = this._uof.CurrentUser.UserID;
                entity.ModuleID = this._uof.ModuleId;
                entity.Id = DataProvider.Instance().Add(entity, this._uof.PortalId);
            }
            catch (SqlException exc)
            {
                if (exc.Message.Contains("fk_TskTm_TaskId"))
                    throw new TaskTimeValidationException("Task not found.",
                        SharedEnum.CrudState.Insert,
                        (int)InsertStatus.TASKNOTFOUND);

                if (exc.Message.Contains("USERCANNOTMODIFYTASK"))
                    throw new TaskTimeValidationException("The user making the change has no acess.",
                        SharedEnum.CrudState.Insert,
                        (int)InsertStatus.USERCANNOTMODIFYTASK);

                if (exc.Message.Contains("ck_TDT_Task_TimeIsValid"))
                    throw new TaskTimeValidationException("End time cannot be less then start time.",
                        SharedEnum.CrudState.Insert,
                        (int)InsertStatus.TIMEISNOTVALID);

                throw exc;
            }
        }

        /// <summary>
        /// Updates a TaskTime
        /// </summary>
        /// <param name="entity">TaskTime to update</param>
        public void Update(_TaskTime entity)
        {
            try
            {
                entity.TaskId = this._taskId;
                entity.LastChangeUserId = this._uof.CurrentUser.UserID;
                entity.ModuleID = this._uof.ModuleId;

                DataProvider.Instance().Update(entity, this._uof.PortalId);
            }
            catch (SqlException exc)
            {
                if (exc.Message.Contains("fk_TskTm_TaskId"))
                    throw new TaskTimeValidationException("Task not found.",
                        SharedEnum.CrudState.Update,
                        (int)UpdateStatus.TASKNOTFOUND);

                if (exc.Message.Contains("USERCANNOTMODIFYTASK"))
                    throw new TaskTimeValidationException("The user making the change has no acess.",
                        SharedEnum.CrudState.Update,
                        (int)UpdateStatus.USERCANNOTMODIFYTASK);

                if (exc.Message.Contains("ck_TDT_Task_TimeIsValid"))
                    throw new TaskTimeValidationException("End time cannot be less then start time.",
                        SharedEnum.CrudState.Update,
                        (int)UpdateStatus.TIMEISNOTVALID);

                throw exc;
            }
        }

        /// <summary>
        /// Deletes an existing TaskTime
        /// </summary>
        /// <param name="entity">TaskTime to delete</param>
        public void Delete(_TaskTime entity)
        {
            try
            {
                entity.TaskId = this._taskId;
                entity.ModuleID = this._uof.ModuleId;
                DataProvider.Instance().Delete(entity, this._uof.PortalId, this._uof.CurrentUser.UserID);
            }
            catch (SqlException exc)
            {
                if (exc.Message.Contains("USERCANNOTMODIFYTASK"))
                    throw new TaskTimeValidationException("The user making the change has no acess.",
                        SharedEnum.CrudState.Delete,
                        (int)DeleteStatus.USERCANNOTMODIFYTASK);

                throw exc;
            }
        }

        #endregion IRepository<_TaskTime> Members

        #region "Crud Enums"

        public enum InsertStatus
        {
            TASKNOTFOUND = -1,
            USERCANNOTMODIFYTASK = -2,
            TIMEISNOTVALID = -3
        }

        public enum UpdateStatus
        {
            TASKNOTFOUND = -1,
            USERCANNOTMODIFYTASK = -2,
            TIMEISNOTVALID = -3
        }

        public enum DeleteStatus
        {
            USERCANNOTMODIFYTASK = -1
        }

        #endregion "Crud Enums"
    }
}